package com.kscrm.web.service.impl;

import java.util.List;

import com.kscrm.common.core.domain.entity.SysUser;
import com.kscrm.common.exception.ServiceException;
import com.kscrm.common.utils.SecurityUtils;
import com.kscrm.common.utils.StringUtils;
import com.kscrm.common.utils.bean.BeanValidators;
import com.kscrm.system.mapper.SysUserMapper;
import com.kscrm.system.service.ISysUserService;
import com.kscrm.web.criteria.ChannelCountCriteria;
import com.kscrm.web.domain.ChannelContact;
import com.kscrm.web.dto.AddChannelDto;
import com.kscrm.web.dto.AddNextChannelDto;
import com.kscrm.web.dto.ClientImportDto;
import com.kscrm.web.mapper.ChannelContactMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.kscrm.web.mapper.ChannelMapper;
import com.kscrm.web.domain.Channel;
import com.kscrm.web.service.IChannelService;

import javax.validation.Validator;

/**
 * 渠道Service业务层处理
 *
 * @author Dhq
 * @date 2025-01-21
 */
@Service
public class ChannelServiceImpl implements IChannelService {
    @Autowired
    private ChannelMapper channelMapper;
    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ChannelContactMapper channelContactMapper;


    /**
     * 查询渠道
     *
     * @param id 渠道主键
     * @return 渠道
     */
    @Override
    public Channel selectChannelById(Long id) {
        return channelMapper.selectChannelById(id);
    }

    /**
     * 查询渠道列表
     *
     * @param channel 渠道
     * @return 渠道
     */
    @Override
    public List<Channel> selectChannelList(Channel channel) {
        return channelMapper.selectChannelList(channel);
    }

    /**
     * 新增渠道
     *
     * @param dto 渠道
     * @return 结果
     */
    @Override
    public int insertChannel(AddChannelDto dto) {
        String account = dto.getAccount();
        // 判断该账号是否已注册用户
        SysUser user = sysUserService.selectUserByUserName(account);
        if(user != null){
            throw new ServiceException("该账号已注册为用户，请更换账号");
        }
        // 插入用户表
        Long userId = sysUserService.registerChannelUser(account,dto.getName());
        Channel channel = new Channel()
                .setUserId(userId)
                .setCode(dto.getCode())
                .setName(dto.getName())
                .setCityName(dto.getCityName())
                .setCityCode(dto.getCityCode())
                .setType(dto.getType())
                .setParentId(dto.getParentId())
                .setBusinessUserId(dto.getBusinessUserId())
                .setProvinceCode(dto.getProvinceCode());
        Long channelId = SecurityUtils.getChannelId();
        if (channelId != null) {
            channel.setParentId(channelId);
        }
        int row = channelMapper.insertChannel(channel);
        if (row == 0) {
            throw new RuntimeException("新增失败：该用户已是渠道");
        }
        Long id = channel.getId();
        if (id == null) {
            throw new RuntimeException("新增失败：渠道编号为空");
        }
        for (ChannelContact c : dto.getContactList()) {
            c.setChannelId(id);
            channelContactMapper.insertChannelContact(c);
        }
        return row;
    }


    @Override
    public int insertNextChannel(AddNextChannelDto dto){
        Channel channel = new Channel()
                .setUserId(dto.getUserId())
                .setCode(dto.getCode())
                .setName(dto.getName())
                .setCityName(dto.getCityName())
                .setCityCode(dto.getCityCode())
                .setType(dto.getType())
                .setParentId(SecurityUtils.getChannelId())
                .setProvinceCode(dto.getProvinceCode());
        int row = channelMapper.insertChannel(channel);
        if (row == 0) {
            throw new RuntimeException("新增失败：该用户已是渠道");
        }
        Long id = channel.getId();
        if (id == null) {
            throw new RuntimeException("新增失败：渠道编号为空");
        }
        for (ChannelContact c : dto.getContactList()) {
            c.setChannelId(id);
            channelContactMapper.insertChannelContact(c);
        }
        return row;
    }

    /**
     * 修改渠道
     *
     * @param dto 渠道
     * @return 结果
     */
    @Override
    public int updateChannel(AddChannelDto dto) {
        Channel channel = new Channel()
                .setId(dto.getId())
//                .setUserId(dto.getUserId())
//                .setCode(dto.getCode())
                .setName(dto.getName())
                .setCityName(dto.getCityName())
                .setCityCode(dto.getCityCode())
                .setParentId(dto.getParentId())
                .setBusinessUserId(dto.getBusinessUserId())
                .setType(dto.getType())
                .setProvinceCode(dto.getProvinceCode());
        int row = channelMapper.updateChannel(channel);
        if (row == 0) {
            throw new RuntimeException("修改失败：渠道信息不存在");
        }
        for (ChannelContact c : dto.getContactList()) {
            channelContactMapper.updateChannelContact(c);
        }
        return row;
    }

    @Override
    public int updateChannelNumber(Channel channel){
        return channelMapper.updateChannel(channel);
    }

    /**
     * 批量删除渠道
     *
     * @param ids 需要删除的渠道主键
     * @return 结果
     */
    @Override
    public int deleteChannelByIds(Long[] ids) {
        return channelMapper.deleteChannelByIds(ids);
    }

    /**
     * 删除渠道信息
     *
     * @param id 渠道主键
     * @return 结果
     */
    @Override
    public int deleteChannelById(Long id) {
        return channelMapper.deleteChannelById(id);
    }


    /**
     * 查询渠道数量
     *
     * @param criteria 条件
     * @return 数量
     */
    @Override
    public int selectChannelCount(ChannelCountCriteria criteria){
        return channelMapper.selectChannelCount(criteria);
    }


}
